<!--suppress ALL -->
<include file="./Template/header.html" />

<link rel="stylesheet" type="text/css" href="/Template/css/hospitalOrder.css?v=2021062102">
<script type="text/javascript" src="/Template/js/amazeui.js"></script>

<script type="text/javascript">


    $(function() {

        //初始化页面配置
//        initPage("订单确认");
        initPage("【{$hospitalOperation.hospital_operation_name}】订单");

        var app = new Vue({
            el: '#container',
            data: {
                number: 0,
                hospitalProductList: [],
                sizeNum: [],
                num:0,
                submit:0
            },
            methods: {
                checkRole: function(employeeRoleId, roleId) {
                    var employeeRoleArr = employeeRoleId.split(",");
                    var roleArr = roleId.split(",");

                    var result = false;
                    $.each(roleArr, function(i,val){
                        if($.inArray(val, employeeRoleArr) != -1 || $.inArray((parseInt(val)+100).toString(), employeeRoleArr) != -1){
                            result = true;
                        }
                    });
                    return result;
                },
                showNameCode: function() {
                    var text = $("#showNameCode").html();
                    if(text == "显示编码"){
                        $(".product_name").hide();
                        $(".product_code").show();
                        $("#showNameCode").html("显示名称");
                    }
                    if(text == "显示名称"){
                        $(".product_name").show();
                        $(".product_code").hide();
                        $("#showNameCode").html("显示编码");
                    }
                },
                calculate: function(num, index) {
                    if((num+app.sizeNum[index])>=0) {
                        app.number += num;
                        app.sizeNum[index] += num;
                        var sizeName = "sizeNum_"+ index;
                        var productName = "productName_"+ index;
                        var productCode = "productCode_"+ index;
                        document.getElementById(sizeName).innerHTML = app.sizeNum[index];
                        if(app.sizeNum[index] == 0){
                            document.getElementById(sizeName).style.color="black";
                            document.getElementById(productName).style.color="black";
                            document.getElementById(productCode).style.color="black";
                        }else{
                            document.getElementById(sizeName).style.color="red";
                            document.getElementById(productName).style.color="red";
                            document.getElementById(productCode).style.color="red";
                        }
                        if("{$hospitalOrder.status}" == "-1") {
                            var numOri = $("input[name=sizeNum_ori_" + index + "]").val();
                            if (app.sizeNum[index] != numOri) {
                                document.getElementById(sizeName).style.background = "#fbc5c4";
                                document.getElementById(productName).style.background = "#fbc5c4";
                                document.getElementById(productCode).style.background = "#fbc5c4";
                            } else {
                                document.getElementById(sizeName).style.background = "#ffffff";
                                document.getElementById(productName).style.background = "#ffffff";
                                document.getElementById(productCode).style.background = "#ffffff";
                            }
                        }
                    }

                },
                allAddOne: function () {
                    if(!confirm("请确认是否全部+1？")){
                        return false;
                    }else{
                        for(var i=0;i<app.hospitalProductList.length;i++) {
                            this.calculate(1,i);
                        }
                    }

                },

                cancelOrder: function () {
                    if(!isNullOrEmpty("{$hospitalOrder.hospital_order_id}")) {
                        window.location.href = "/wechat/hospitalProduct/hospitalOrderList";
                    }else{
                        window.location.href = "/wechat/hospitalProduct/hospital/id/{$hospital.hospital_id}";
                    }
                },

                saveOrder: function(saveStatus) {

                    var hospitalOrderId = "{$hospitalOrder.hospital_order_id}";

                    var number = app.number;
                    var createDate = $("input[name=createDate]").val();
                    var opDate = $("input[name=opDate]").val();
                    var subhospitalName = $("input[name=subhospitalName]").val();
                    var doctorName = $("input[name=doctorName]").val();
                    var patientName = $("input[name=patientName]").val();

                    var note = $("input[name=note]").val();

                    var productList = "";
                    var numberList = "";
                    var reportList = "";
                    for(var i=0;i<app.sizeNum.length;i++){
                        if(app.sizeNum[i] > 0) {
                            if(!isNullOrEmpty(numberList)) {
                                numberList += ",";
                                productList += ",";
                                reportList += ",";
                            }
                            numberList += app.sizeNum[i];
                            productList += app.hospitalProductList[i].hospital_product_id;
                            reportList += (app.hospitalProductList[i].hospital_product_name + " x " + app.sizeNum[i]);
                        }
                    }

                    var data = {};

                    if(saveStatus != -1){
                        if(number == 0) {
                            alert("未选择产品");
                            return false;
                        }

                        if(isNullOrEmpty(createDate)) {
                            alert("请选择下单日期");
                            return false;
                        }

                        if(isNullOrEmpty(opDate)) {
                            alert("请选择手术日期");
                            return false;
                        }

                        if("{$hospital.has_subhospital}" == "1" && isNullOrEmpty(subhospitalName)) {
                            alert("请填写手术医院名称");
                            return false;
                        }

                        if(isNullOrEmpty(doctorName)) {
                            alert("请填写手术医生姓名");
                            return false;
                        }

                        if(isNullOrEmpty(patientName)) {
                            alert("请填写病人姓名");
                            return false;
                        }
                    }

                    data.saveStatus = saveStatus;
                    data.hospitalOrderId = hospitalOrderId;
                    data.hospitalOperationId = "{$hospitalOperation.hospital_operation_id}";
                    data.productList = productList;
                    data.numberList = numberList;
                    data.reportList = reportList;
                    data.number = number;
                    data.createDate = createDate;
                    data.opDate = opDate;
                    if("{$hospital.has_subhospital}" == "1")
                        data.subhospitalName = subhospitalName;
                    data.doctorName = doctorName;
                    data.patientName = patientName;
                    data.note = note;


                    var diffText = "";
                    if(reportList != "{$hospitalOrder.report_list}") {
                        diffText += "* 订单列表\r\n";
                    }
                    if(createDate != "{$hospitalOrder.create_date}") {
                        diffText += "* 下单日期\r\n";
                    }
                    if(opDate != "{$hospitalOrder.op_date}") {
                        diffText += "* 手术日期\r\n";
                    }
                    if("{$hospital.has_subhospital}" == "1" && subhospitalName != "{$hospitalOrder.subhospital_name}") {
                        diffText += "* 手术医院\r\n";
                    }
                    if(doctorName != "{$hospitalOrder.doctor_name}") {
                        diffText += "* 手术医生\r\n";
                    }
                    if(patientName != "{$hospitalOrder.patient_name}") {
                        diffText += "* 病人姓名\r\n";
                    }
                    if(note != "{$hospitalOrder.note}") {
                        diffText += "* 备注\r\n";
                    }

                    var confirmText = "";
                    switch(saveStatus){
                        case -1:
                            if("{$hospitalOrder.status}" == -1){
                                confirmText = "请确认是否仅【暂存】订单？（【出库】之前可再次修改订单内容）";
                                if(!isNullOrEmpty(diffText)){
                                    confirmText += ("\r\n\r\n以下内容有所修改：\r\n" + diffText);
                                }
                            }else{
                                confirmText = "请确认是否仅【暂存】订单？（【提交】之前可再次修改订单内容）";
                                if("{$hospitalOrder.status}" == -2 && !isNullOrEmpty(diffText)){
                                    confirmText += ("\r\n\r\n以下内容有所修改：\r\n" + diffText);
                                }
                            }

                            break;
                        case 0:
                            confirmText = "请确认是否【提交】订单，之后将不能再次修改订单内容？"
                            if("{$hospitalOrder.status}" == -2 && !isNullOrEmpty(diffText)){
                                confirmText += ("\r\n\r\n以下内容有所修改：\r\n" + diffText);
                            }
                            break;
                        case 1:
                            if(!isNullOrEmpty(diffText)){
                                confirmText = "请确认是否修改以下尚未出库的订单内容，并提前与出库人协商？\r\n\r\n" + diffText;
                            }else{
                                alert("订单并没有改动，无需提交");
                                return false;
                            }
                            break;
                        case 2:
                            confirmText = "请确认是否【出库】？（之后将无法修改订单内容）"
                            if(!isNullOrEmpty(diffText)){
                                confirmText += ("\r\n\r\n以下内容有所修改：\r\n" + diffText);
                            }

                            break;
                    }
                    if(confirm(confirmText)){
                        $.ajax({
                            url:"/wechat/hospitalProduct/saveOrder",
                            type:"post",
                            data:data,
                            dataType:"JSON",
                            beforeSend:function(){
                                $("#loadingToast").show();
                                $("#submitBtn").hide();
                            },
                            complete:function(){
                                $('#loadingToast').hide();
                            },
                            success:function(data){
                                if(data.code != 200) {
                                    alert(data.msg);
                                    window.location.href = "/wechat/hospitalProduct/hospitalOrderList";
                                }else{
                                    window.location.href = "/wechat/hospitalProduct/hospitalOrderList";
                                }

                            },
                            error:function(data){
                                window.location.href = "/wechat/hospitalProduct/hospitalOrderList";
                            }
                        })
                    }else{
                        return;
                    }
                },

            }
        })


        function getHospitalProductList() {

            $.ajax({
                url:"/wechat/hospitalProduct/getHospitalProductList",
                type:"post",
                data:{
                    hospitalOrderId: "{$hospitalOrderId}",
                    hospitalOperationId: "{$hospitalOperation.hospital_operation_id}",
                },
                dataType:"JSON",
                beforeSend:function(){
                    $("#loadingToast").show();
                },
                complete:function(){
                    $('#loadingToast').hide();
                },
                success:function(data){
                    var productList = "{$hospitalOrder.product_list}".split(",");
                    var numberList = "{$hospitalOrder.number_list}".split(",");

                    app.hospitalProductList = data;
                    for(var j=0;j<app.hospitalProductList.length;j++){
                        app.sizeNum[j] = 0;
                        for(var k=0;k<productList.length;k++) {
                            if (app.hospitalProductList[j].hospital_product_id == productList[k]) {
                                app.sizeNum[j] = parseInt(numberList[k]);
                                app.number += app.sizeNum[j];
                            }
                        }
                    }
                },
            })
        }

        getHospitalProductList();

        if("{$hospitalOrder.status}" == "-1"){
            $("input[name='createDate']").change(function () {
                if ($("input[name=createDate]").val() != "{$hospitalOrder.create_date}") {
                    $("input[name=createDate]").css('background-color', "#fba38f");
                }else {
                    $("input[name=createDate]").css('background-color', "#ffffff");
                }
            });

            $("input[name='opDate']").change(function () {
                if ($("input[name=opDate]").val() != "{$hospitalOrder.op_date}") {
                    $("input[name=opDate]").css('background-color', "#fba38f");
                }else {
                    $("input[name=opDate]").css('background-color', "#ffffff");
                }
            });

            $("input[name='doctorName']").keyup(function () {
                if ($("input[name=doctorName]").val() != "{$hospitalOrder.doctor_name}") {
                    $("input[name=doctorName]").css('background-color', "#fbc5c4");
                }else {
                    $("input[name=doctorName]").css('background-color', "#ffffff");
                }
            });

            $("input[name='patientName']").keyup(function () {
                if ($("input[name=patientName]").val() != "{$hospitalOrder.patient_name}") {
                    $("input[name=patientName]").css('background-color', "#fbc5c4");
                }else {
                    $("input[name=patientName]").css('background-color', "#ffffff");
                }
            });

            $("input[name='note']").keyup(function () {
                if ($("input[name=note]").val() != "{$hospitalOrder.note}") {
                    $("input[name=note]").css('background-color', "#fbc5c4");
                }else {
                    $("input[name=note]").css('background-color', "#ffffff");
                }
            });
        }


        $(".item select").change(function() {
            var value = $(this).find("option:selected").text();
            $(this).prev().val(value);
        });

        $("img.icon-up").click(function() {
            $(this).hide();
            $("img.icon-down").show();

            $(".size-detail").toggle();
            $(".size-comment").toggle();

        });

        $("img.icon-down").click(function() {

            $(this).hide();
            $("img.icon-up").show();

            $(".size-detail").toggle();
            $(".size-comment").toggle();
        });
        $("#checkOutstock").change(function() {
            if($(this).is(":checked")){
                $("#submitBtn").show();
                $("#cartBtn").hide();
            }else{
                $("#submitBtn").hide();
                $("#cartBtn").show();
            }
        });
    })

</script>

<div class="container" id="container">
    <div class="product-detail">
        <!--<img class="product-img" src="{$finishedProduct.image_url}" alt="product">-->
        <div class="detail-info">
            <p class="product-price" id="productPrice">共计：<a style="color: #e97a15">{{number}}</a> 件</p>
            <p class="product-name"><a style="color: #3fcca9; font-weight: bold" v-on:click="allAddOne()">{$hospital.hospital_name}</a> <a style="color: #7171cc; font-weight: bold">【{$hospitalOperation.hospital_operation_name}】</a></p>
            <!--<p class="number"></p>-->
            <p class="buy-time"><a style="color: black;">更新人：</a>{$updater.admin_name}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a style="color: black;">更新时间：</a>{$hospitalOrder.update_time}</p>
        </div>
    </div>
    <div class="number-and-transport">
        <div class="size">
            <div class="item" style="margin-bottom: 0.5rem;height:3rem;border-bottom:0px">
                订单列表
                <button class="name-code-button" id="showNameCode" v-on:click="showNameCode()">显示编码</button>
                <img class="icon-up" src="/Template/images/icon-up.png" alt="">
                <img class="icon-down" src="/Template/images/icon-down.png" alt="">
                <!--span style="float:right;font-size:0.8rem;overflow: hidden;text-overflow: ellipsis;">{$finishedProduct.size_comment}</span-->
            </div>
            <div v-for="hospitalProduct in hospitalProductList" class="size-detail">
                <div class="item-small">
                    <span class="sum-number product_name" id="productName_{{ $index }}" v-if="sizeNum[$index] == 0" style="color: black;">{{hospitalProduct.hospital_product_name}}</span>
                    <span class="sum-number product_name" id="productName_{{ $index }}" v-if="sizeNum[$index] != 0" style="color: red;">{{hospitalProduct.hospital_product_name}}</span>
                    <span class="sum-number product_code" id="productCode_{{ $index }}" v-if="sizeNum[$index] == 0" style="color: black;display: none">{{hospitalProduct.code}}</span>
                    <span class="sum-number product_code" id="productCode_{{ $index }}" v-if="sizeNum[$index] != 0" style="color: red;display: none">{{hospitalProduct.code}}</span>
                    <span class="number-change">
                        <img class="icon-plus" v-on:click="calculate(-1, $index)" src="/Template/images/minus.png" alt="">
                        <span class="sum-number" id="sizeNum_{{ $index }}" v-if="sizeNum[$index] == 0" style="color: black;">{{sizeNum[$index]}}</span>
                        <span class="sum-number" id="sizeNum_{{ $index }}" v-if="sizeNum[$index] != 0" style="color: red;">{{sizeNum[$index]}}</span>
                        <input type="hidden" name="sizeNum_ori_{{ $index }}" value="{{sizeNum[$index]}}">
                        <img class="icon-plus" v-on:click="calculate(1, $index)" src="/Template/images/plus.png" alt="">
                    </span>
                </div>
            </div>
        </div>
    </div>
    <!--div class="customer-info agent">
        <div class="item">
            <span>客户名称</span>
            <input type="text" name="customerName" placeholder="请填写客户名称">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
    </div-->
    <div class="patient-info agent">
        <div class="item">
            <span>下单日期</span>
            <input type="date" name="createDate" placeholder="" value="{$hospitalOrder.create_date}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
        <div class="item">
            <span>手术日期</span>
            <input type="date" name="opDate" placeholder="" value="{$hospitalOrder.op_date}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
        <div v-if="{$hospital.has_subhospital} == 1" class="item">
            <span>手术医院</span>
            <input type="text" name="subhospitalName" placeholder="" value="{$hospitalOrder.subhospital_name}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
        <div class="item">
            <span>手术医生</span>
            <input type="text" name="doctorName" placeholder="" value="{$hospitalOrder.doctor_name}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
        <div class="item">
            <span>病人姓名</span>
            <input type="text" name="patientName" placeholder="" value="{$hospitalOrder.patient_name}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
        <div class="item">
            <span>备注</span>
            <input type="text" name="note" placeholder="请编辑备注信息" value="{$hospitalOrder.note}">
            <img class="icon-right" src="/Template/images/icon-right.png" alt="">
        </div>
    </div>
    <div class="product-need">
        <div class="item" v-if="'{$creator.admin_name}' != ''">
            <span>下单人</span>
            <input type="text" name="receiveName" placeholder="" value="{$creator.admin_name}" readonly>
            <!--<img class="icon-right" src="/Template/images/icon-right.png" alt="">-->
        </div>
        <div class="item" v-if="{$hospitalOrder.status} == 1">
            <span>出库</span>
            <input type="text" name="receiveName" placeholder="" value="{$hospitalOrder.outstock_time} {$deliver.admin_name}" readonly>
            <!--<img class="icon-right" src="/Template/images/icon-right.png" alt="">-->
        </div>
        <div v-if="{$emptyOrder} == 1">
            <div class="checkline">
                <input class="checkFirst" id="checkOutstock" value="0" type="checkbox" ><a style="font-weight: bold; color: #d92f3b">确认提交</a>
            </div>
            <div class="line"></div>
        </div>
        <div v-if="{$hospitalOrder.status} == -2">
            <div class="checkline">
                <input class="checkFirst" id="checkOutstock" value="0" type="checkbox" ><a style="font-weight: bold; color: #d92f3b">确认提交</a>
            </div>
            <div class="line"></div>
        </div>
        <div v-if="{$hospitalOrder.status} == -1 && checkRole('{$user.role}', '1,13')">
            <div class="checkline">
                <input class="checkFirst" id="checkOutstock" value="0" type="checkbox" ><a style="font-weight: bold; color: #d92f3b">确认出库</a>
            </div>
            <div class="line"></div>
        </div>
    </div>



    <div class="order-operation">
        <p style="margin-left: 20%;width: 40%">共 <a style="color: #e97a15">{{number}}</a> 件</p>
        <button class="cancel-order" v-on:click="cancelOrder">返 回</button>

        <div v-if="{$emptyOrder} == 1">
            <button id="submitBtn" style="display: none" class="submit-order" v-on:click="saveOrder(0)">提 交</button>
            <button id="cartBtn" class="submit-cart" v-on:click="saveOrder(-1)">暂 存</button>
        </div>
        <div v-if="{$hospitalOrder.status} == -2 && {$creator.employee_id} =={$user.employee_id}">
            <button id="submitBtn" style="display: none" class="submit-order" v-on:click="saveOrder(0)">提 交</button>
            <button id="cartBtn" class="submit-cart" v-on:click="saveOrder(-1)">暂 存</button>
        </div>

        <button id="cartBtn" class="submit-cart" v-on:click="saveOrder(-1)" v-if="{$hospitalOrder.status} == -1 && checkRole('{$user.role}', '1,13')">暂 存</button>
        <button id="submitBtn" class="submit-order" style="display: none" v-on:click="saveOrder(2)" v-if="{$hospitalOrder.status} == -1 && checkRole('{$user.role}', '1,13')">出 库</button>
    </div>
</div>
<include file="./Template/footer.html" />